package com.itboye.ihomebank.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.KeyFragment;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.PersonDataBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.util.controller.LoginController;
import com.itboye.ihomebank.util.controller.LoginedState;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.SavePersonData;

import java.util.Observable;
import java.util.Observer;

/**
 * 通过旧密码修改密码
 */
public class ActivityJiDeUserPass extends BaseOtherActivity implements Observer{
    View v_statusbar;
    TextView add_shap_title_tv;
    ImageView close_icon;
    EditText old_pass,new_pass,new_pass02;
    TextView pass_ok;

    UserPresenter userPresenter;

    String phonenumber;
    String newPass;
    @Override
    protected int layout_id() {
        return R.layout.activity_ji_de_pass;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("修改密码");
        userPresenter=new UserPresenter(this);

        phonenumber = (String) SPUtils.get(this, null,SPContants.MOBILE, "");
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.pass_ok:
                String oldPass= old_pass.getText().toString();
                newPass= new_pass.getText().toString();
                String newPass02= new_pass02.getText().toString();
                if(oldPass.equals("")){
                    ByAlert.alert("请输入旧密码");
                    return;
                }
                if(newPass.equals("")){
                    ByAlert.alert("请输入新密码密码");
                    return;
                }
                if(newPass02.equals("")){
                    ByAlert.alert("请再次输入新密码");
                    return;
                }
                if(!newPass.equals(newPass02)){
                    ByAlert.alert("两次输入不一致");
                    return;
                }
                String  uid = SPUtils.get(this, null, SPContants.USER_ID, "")+ "";
                showProgressDialog("请稍后",true);
                userPresenter.updatePwdByPwd(uid,oldPass,newPass);

                break;

            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.updatepwdbypwd_success) {
                ByAlert.alert("修改成功");

                userPresenter.login(phonenumber, newPass);
            }
            if (entity.getEventType() == UserPresenter.updatepwdbypwd_fail) {
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == UserPresenter.user_success) {
                PersonDataBean bean = (PersonDataBean) entity.getData();
                if (bean != null) {
                    SavePersonData.saveData(ActivityJiDeUserPass.this,bean);

                    finish();

                    LoginController.setLoginState(new LoginedState());

                    KeyFragment.getInstance().panduan(bean.getUid());
                }

            } else if (entity.getEventType() == UserPresenter.user_fail) {
                ByAlert.alert(entity.getData());

            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {

        }
    }
}
